package com.easysoft.framework.action;

import com.easysoft.commons.utils.StringUtils;
import com.easysoft.commons.utils.WebConstants;
import com.easysoft.framework.annotation.logs.SystemControllerLog;
import com.easysoft.framework.beans.Page;
import com.easysoft.framework.dict.model.SysDictionary;
import com.easysoft.framework.dict.model.SysDictionaryQuery;
import com.easysoft.framework.dict.model.SysDictionaryView;
import com.easysoft.framework.dict.service.SysDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by try on 2017/8/1.
 */

@RestController
@RequestMapping(SystemDictionaryAction.BASE_VIEW)
public class SystemDictionaryAction {


    public static final String BASE_VIEW = "/dict";
    private final static Logger logger = LoggerFactory.getLogger(SystemDictionaryAction.class);

    @Autowired
    @Qualifier("service.SysDictionaryService")
    private SysDictionaryService sysDictionaryService;


    /**
     * 获取字典列表
     *
     * @param page
     * @param query
     * @return
     */
    @RequestMapping(value = "/listDictionary", method = RequestMethod.GET)
    public ModelAndView listDictionary(Page page, SysDictionaryQuery query) {
        logger.info("Call SysDictionaryAction.listDictionary({},{})", page.toString(), query.toString());
        List<SysDictionaryView> listDictionary = sysDictionaryService.selectSysDictionaryListByPage(page, query);
        ModelAndView mv = new ModelAndView(BASE_VIEW + "/listDictionary");
        mv.addObject("page", page); // 返回分页
        mv.addObject("query", query);// 查询条件
        mv.addObject("listDictionary", listDictionary); // 返回查询结果集
        return mv;
    }

    @RequestMapping(value = "/editDictionary", method = RequestMethod.GET)
    public ModelAndView editDictionary(String action, @RequestParam(required = false) String id) {
        logger.info("Call SysDictionaryAction.editDictionary({},{})", action, id);
        ModelAndView mv = new ModelAndView(BASE_VIEW + "/editDictionary");
        List<SysDictionaryView> dictionaryViewList = sysDictionaryService.selectSysDictionaryWithoutParent();
        mv.addObject("parents", dictionaryViewList);
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("add")) {
            // 添加
            if (StringUtils.isNotNullAndBlank(id)) {
                return null;
            }
            return mv;
        }
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("update")) {
            // 修改
            if (StringUtils.isNullOrBlank(id)) {
                return null;
            }
            SysDictionary entity = sysDictionaryService.selectSysDictionary(id);
            if (entity == null) {
                return null;
            }
            mv.addObject("entity", entity);
            return mv;
        }
        return null;
    }

    @RequestMapping(value = "saveDictionary", method = RequestMethod.POST)
    public int saveDictionary(String action, SysDictionary sysDictionary, HttpSession session) {
        logger.info("Call SystemManagerAction.saveManager({},{})", action, sysDictionary.toString());
        if (sysDictionary.getParentId().equals("0")) {
            sysDictionary.setParentId("");
        }
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("add")) {
            // 添加
            if (StringUtils.isNotNullAndBlank(sysDictionary.getId())) {
                return 0;
            }
            return addSysDictionary(sysDictionary, session);
        }
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("update")) {
            // 修改
            if (StringUtils.isNullOrBlank(sysDictionary.getId())) {
                return 0;
            }
            return updateSysDictionary(sysDictionary, session);
        }
        return 0;
    }


    /**
     * 查询所有字典
     *
     * @return
     */
    @RequestMapping(value = "/selectSysDictionary", method = RequestMethod.POST)
    public List<SysDictionaryView> selectSysDictionary() {
        logger.info("Call SysDictionaryAction.selectSysDictionary()");

        return sysDictionaryService.selectSysDictionary();
    }

    /**
     * 添加字典
     *
     * @param sysDictionary 单页信息
     */
    @SystemControllerLog(description = "添加字典")
    private int addSysDictionary(SysDictionary sysDictionary, HttpSession session) {
        logger.info("Call SysDictionaryAction.addSysDictionary()");
        sysDictionary.setOperator((String) session.getAttribute(WebConstants.SESSION_USER_ID));
        return sysDictionaryService.addSysDictionary(sysDictionary);
    }

    /**
     * 更新字典信息
     *
     * @param sysDictionary 字典信息
     */
    @SystemControllerLog(description = "更新字典信息")
    private int updateSysDictionary(SysDictionary sysDictionary, HttpSession session) {
        logger.info("Call SysDictionaryAction.updateSysDictionary()");
        sysDictionary.setOperator((String) session.getAttribute(WebConstants.SESSION_USER_ID));
        return sysDictionaryService.updateSysDictionaryById(sysDictionary);
    }

}
